<?php


namespace app\common\library;

class Express
{

    private $secretKey = 'eQKgbOUu5434';
    private $customer = '675130720336687F338EFFE0696D87AC';
    private $secret = '965a99eb89784587b6382c6ba2e5e602';
    private $userid = 'f3f3cbd61058490e97110b6944079278';
	private $callbackUrl = '/api/callback/kuaidi';

    public function __construct()
    {
        $this->callbackUrl = full_url($this->callbackUrl);
    }
	
	/**
	 * 生成加密串
	 * @param {Object} $number 快递号
	 */
    public function sign($number)
    {
        return md5(hash_hmac('sha1', $number, $this->secretKey, true));
    }
	
	/**
	 * 订阅快递
	 * @param {Object} $data 快递数组
	 */
	public function subScribe($company = '' ,$number = 0 ,$phone = 0)
	{
		//参数设置
	    $param = array (
			'company' => $company,//快递公司编码
			'number' => $number,//快递单号
			'key' => $this->secretKey, 
			'parameters' => array (
				'callbackurl' => $this->callbackUrl,//回调地址
				'salt' => $this->sign($number),
				'phone' => $phone,
				'resultv2' => '1',//行政区域解析
				'autoCom' => '0'//单号智能识别
			)
		);
		//请求参数
	    $post_data = array();
	    $post_data["schema"] = 'json';
	    $post_data["param"] = json_encode($param);
		$params = "";
	    foreach ($post_data as $k=>$v) {
	        $params .= "$k=".urlencode($v)."&";	//默认UTF-8编码格式
	    }
	    $post_data = substr($params, 0, -1);
		// 请求数据
		$result = $this->curlPost('http://poll.kuaidi100.com/poll', $post_data);
		$data = str_replace("\"", '"', $result );
		$data = json_decode($data, true); 
		return $data;
	}


    /**
     * @param $request
     * 获取物流
     */
    public function getLogistics($com, $num)
    {
        //客户授权key
        $key = $this->secretKey;
        //查询公司编号
        $customer = $this->customer;
        $param = array(
            'com' => $com,//快递公司编码
            'num' => $num, //快递单号
            'phone' => '',//手机号
            'from' => '',//出发地城市
            'to' => '', //目的地城市
            'resultv2' => '1',//开启行政区域解析
            'order' => 'desc'
        );

        //请求参数
        $post_data = array();
        $post_data["customer"] = $customer;
        $post_data["param"] = json_encode($param);
        $sign = md5($post_data["param"] . $key . $post_data["customer"]);
        $post_data["sign"] = strtoupper($sign);
        $url = 'http://poll.kuaidi100.com/poll/query.do';    //实时查询请求地址
        $headers['Content-Type'] = 'application/x-www-form-urlencoded';
        $client = new \GuzzleHttp\Client();
        $response = $client->request('POST', $url, [
            'headers' => $headers,
            'form_params' => $post_data
        ]);
        // $response = $client->post($url,$post_data);
        $code = $response->getStatusCode(); # 200
        $info = $response->getBody(); # '{"id": 1420053, "name": "guzzle", ...}'
        if ($code != 200) {
            return ['code' => 0, 'msg' => '请求失败'];
        }
        $data = json_decode($info, true);
        return ['code' => 1, 'msg' => '', 'data' => $data];
    }

	
	/**
	 * POST请求
	 * @param {Object} $post_url
	 * @param {Object} $post_data
	 */
	public function curlPost($post_url, $post_data)
	{
	    $ch = curl_init();
	    curl_setopt($ch, CURLOPT_POST, 1);
	    curl_setopt($ch, CURLOPT_HEADER, 0);
	    curl_setopt($ch, CURLOPT_URL, $post_url);
	    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
	    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	    return curl_exec($ch);
	}
	
	
}
